package org.zhs.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.zhs.pojo.Department;
import org.zhs.pojo.Employee;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
    //主键自增
    private static Integer initialId = 1006;
    //获取部门Dao类,用于CRUD
    @Autowired
    private DepartmentDao departmentDao;

    private static Map<Integer, Employee> employees;
//    将dao类赋值到该类中

    static {
        employees = new HashMap<>(4);
        employees.put(1001, new Employee(1001, "zhu", "122@qq.com", 1, new Department(101, "教师部")));
        employees.put(1002, new Employee(1002,"zhang","123@qq.com",1,new Department(101,"教师部")));
        employees.put(1003, new Employee(1003,"li","124@qq.com",1,new Department(101,"教师部")));
        employees.put(1004, new Employee(1004,"wang","125@qq.com",1,new Department(101,"教师部")));
        employees.put(1005, new Employee(1005,"liu","126@qq.com",1,new Department(101,"教师部")));
    }
//    增加员工
    public String add(Employee employee){
        if (employee.getId() == null){
            employee.setId(initialId++);
        }
//        先获取穿参的部门id,然后通过部门id来获取部门对象,最后设置对象
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
        return "Done";
    }
//查询全部员工
    public Collection<Employee> getEmployees(){
        return employees.values();
    }
//    查询员工
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }
    public String deleteEmployeeById(Integer id){
        employees.remove(id);
        return "Done";
    }
}
